
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 *
 * @author David Beaver
 */
public class ServerConfig {
    private Integer port;
    private Integer ftpPort;
    private boolean hasGPU;
    private boolean isPublic;
    private Integer MAXTHREADS;
    private HashMap<String,String> aliasMap;

    public ServerConfig(){
        this.port = new Integer(0);
        this.ftpPort = new Integer(0);
        this.hasGPU = false;
        this.isPublic = false;
        this.MAXTHREADS = 2;
        this.aliasMap = new HashMap<String,String>();
    }
    
    public boolean isAlias(String relPath){
        if(this.aliasMap.containsKey(relPath)){
            return true;
        }
        return false;
    }
    public String printConfig(){
        String config = new String();
        config += "\t[2]EXEC Port: "+this.port+"\n";
        config += "\t[3]FTP Port: "+this.ftpPort+"\n";
        config += "\t[4]Public: "+this.isPublic+"\n";
        config += "\t[5]GPU support: "+this.hasGPU+"\n";
        config += "\t[6]Max threads: "+this.MAXTHREADS+"\n";
        config += "Mapping aliases...\n";
        int count = 1;
        for(String alias : this.aliasMap.keySet()){
            config += "\t["+count+"]"+alias+" =>"+this.aliasMap.get(alias)+"\n";
            count++;
        }
        return config;
    }
    public boolean isPublic(){
        return this.isPublic;
    }
    public boolean hasGPU(){
        return this.hasGPU;
    }
    public ServerConfig(String path){
        this.loadConfig(path);
    }
    
    public Integer getPort(){
        return this.port;
    }
    
    public Integer getFtpPort(){
        return this.ftpPort;
    }
    public Integer getMaxThreads(){
        return this.MAXTHREADS;
    }
    public String resolveAlias(String alias){
        if(this.aliasMap.containsKey(alias)){
            return this.aliasMap.get(alias);
        }
        return "";
    }
    public String resolvePath(String path){
        String realPath = "";
        StringTokenizer pathParser = new StringTokenizer(path,"/"); //parse the path by directories
        while(pathParser.hasMoreTokens()){ //while there are more directories to check
            String dir = pathParser.nextToken(); //read the next directory
            if(this.isAlias(dir)){ //if its an alis
                realPath += this.resolveAlias(dir); //resolve path for real path
            }else{ //its not an alias
                realPath += dir + File.separator; //add it on real path
            }
        }
        return realPath;
    }
    public void loadConfig(String path){
        try{
            FileInputStream file = new FileInputStream(path); //open the file
            DataInputStream data = new DataInputStream(file);  //prepare data
            BufferedReader contents = new BufferedReader(new InputStreamReader(data)); //prepare to read data
            String line; 
            StringTokenizer options;
            String option, value; //option=>value
            while (!(line = contents.readLine()).isEmpty()){ //if the line isn't empty
                options = new StringTokenizer(line); //read the type of option
                option = options.nextToken(); //save the option
                if(option.charAt(0) != '#'){ //if not a comment
                    if(option.equals("Alias")){ //if the type is an Alias
                        option = options.nextToken(); //save the alias
                        value = options.nextToken(); //save its real value
                        this.aliasMap.put(option,value); //map the values
                        //System.out.println(option+"===>"+value);
                    }else if(option.equals("Listen")){ //if the type is Listen
                        this.port = new Integer(options.nextToken()); //save the port
                        //System.out.println("Port: "+this.port);
                    }else if(option.equals("Ftp-Listen")){ //if the type is Ftp-Listen
                        this.ftpPort = new Integer(options.nextToken()); //save the ftp port
                    }else if(option.equals("GPU")){
                        value = options.nextToken();
                        if(value.equals("YES")){
                            this.hasGPU = true;
                        }else{
                            this.hasGPU = false;
                        }
                    }else if(option.equals("Public")){
                        value = options.nextToken();
                        if(value.equals("YES")){
                            this.isPublic = true;
                        }else{
                            this.isPublic = false;
                        }
                    }else if(option.equals("Max-Threads")){
                        value = options.nextToken();
                        this.MAXTHREADS = new Integer(value);
                    }
                }
            }
            file.close(); //close the file
        }catch (Exception e){
            //System.err.println("Error: " + e.getMessage());
        }
    }
}
